# -*- coding: utf-8 -*-
"""
Created on Mon Apr 21 10:55:56 2025
累加算法器
@author: Bradley
"""
import time
def Add_N(n1):
    total = 0
    for a in range(1,n1+1):
        total = total + a
    return total

N = 900000
t1 = time.process_time()
print('1到%d自然数累加结果为%d'%(N,Add_N(N)))
t2 = time.process_time()
print('循环累加算法用时：%.8f秒'%(t2-t1))  # 取小数点8位，浮点数
#=================累加算法二====================================
t3 = time.process_time()
print('采用累加公式计算1到%d累加和为%d'%(N, N*(N+1)/2)) # 直接采用自然数累加公式，仅一行代码
t4 = time.process_time()
print('循环累加算法二用时：%.12f秒'%(t4-t3))  # 取小数点12位，浮点数
# out:
# 1到900000自然数累加结果为405000450000
# 循环累加算法用时：0.04687500秒
# 采用累加公式计算1到900000累加和为405000450000
# 循环累加算法二用时：0.000000000000秒